package org.agridata.akvisualization.controller.quarantine;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.QuarantineStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "产地检疫及屠宰检疫")
@RequestMapping("/quarantine/")
public class QuarantineStatisticsController {

    @Autowired
    private QuarantineStatisticsService statisticsService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author yanshuanglong
     * @Description 智慧动检
     * @Date 11:38 2023/3/28
     * @Param [times]
     * @return org.agridata.common.Result
     **/
    @ApiOperation("产地检疫及屠宰检疫可视化")
    @PostMapping("origin")
    public Result getOriginStatistics(Integer times){
        Object animalRatioKey = new Object();
        animalRatioKey = redisTemplate.opsForValue().get("getOriginStatisticsKey");
        if (animalRatioKey != null){
            return Result.ok(animalRatioKey);
        }
        Map<String, Object> list = statisticsService.getOriginStatisticsList(times);
        redisTemplate.opsForValue().set("getOriginStatisticsKey",list);
        // 设置 key 的过期时间，例如设置为 1 小时
        redisTemplate.expire("getOriginStatisticsKey", 5, TimeUnit.HOURS);
        return Result.ok(list);

    }

}
